package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsAdmin;
import com.itqf.dto.CommonsDto;
import com.itqf.service.DtsAdminService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.AuthSupport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * projectName: gp_manage
 *
 * @author: 赵伟风
 * time: 2021/8/22 9:25
 * description: 系统模块、管理员管理controller
 */
@RestController
@RequestMapping("admin/admin/")
@CrossOrigin("*")
public class AdminDtsAdminController {

    @Autowired
    private DtsAdminService dtsAdminService;

    /**
     * 查询管理员列表
     * 参数传参  url?参数
     *          1.直接形参列表变量接收
     *          2.实体对象接收 dto
     *
     *
     *   权限定义: 三层结构  数据库给与的权限也是三成
     *            admin:模块:接口后缀 list read creare
     *
     * @return
     */

    //权限限制
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"} ,button = "查询" )
    @GetMapping("list")
    public Object list(CommonsDto commonsDto){

        Map<String,Object> data = dtsAdminService.findAdminByPage(commonsDto);

        return ResponseUtil.ok(data);
    }



    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"} ,button = "创建")
    @PostMapping("create")
    public Object create(@RequestBody DtsAdmin dtsAdmin){


        //1.添加失败
            //账号已经存在
            //密码长度小于6
        //2.添加成功  ResponseUtils.ok(dtsAdmin)

        Map<String,Object> data = dtsAdminService.insertDtsAdmin(dtsAdmin);

        return data;
    }




    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"},button = "修改")
    @PostMapping("update")
    public Object update(@RequestBody DtsAdmin dtsAdmin)
    {

        //1.成功 responseUtils.ok(dtsAdmin --> 修改的时间)

        //2.失败 账号已经存在 / 密码长度小于6

        Object data = dtsAdminService.updateAdminByPrimaryKey(dtsAdmin);

        return data;
    }





    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"},button = "删除")
    @PostMapping("delete")
    public Object delete(@RequestBody DtsAdmin dtsAdmin)
    {

        //1.成功 responseUtils.ok(dtsAdmin --> 修改的时间)

        //2.失败 不能自己登陆账号 [不能删除正在登陆账号]   不能删除超级管理员

        DtsAdmin currentUser = AuthSupport.currentUser();

        Object data = dtsAdminService.deleteAdminById(dtsAdmin,currentUser);

        return data;
    }





}
